from flask import Flask,session,render_template,request
import os
from flask_sqlalchemy import SQLAlchemy
from model import *
from blueprint.user  import user_bp
from blueprint.product import product_bp
from extend import db

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['SECRET_KEY'] = os.urandom(24)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True
app.register_blueprint(user_bp)
app.register_blueprint(product_bp)
db.init_app(app)

with app.app_context():
    db.create_all()

@app.route('/')
def index():
    products=Product.query.all()
    productType=ProductType.query.all()
    return render_template( 'product/index.html',products=products,productType=productType)

@app.route('/search')
def search():
    keyword = request.args.get('keyword')
    productType = ProductType.query.all()
    result = select1(keyword)
    if result:
        return render_template('product/index.html', products=result,productType=productType)

    else:
        return 'Not Found'


def select1(keyword):
    data = Product.query.filter(Product.pname.like('%'+keyword+'%')).all()
    return data


if __name__ == '__main__':
    app.run(debug=True)
